// 解析模板标记的语法
var fs = require('fs');
var path = require('path');
var config = require('../config.js');
var underscore = require('underscore');
module.exports = function(res) {
	res.render = function(ViewName, dataobj) {
		var viewPath;
		// console.log(dataobj);
		// console.log(ViewName.startsWith('bootstrap'));
		if (ViewName.startsWith('bootstrap')) {
			viewPath = path.join(config.cssPath, ViewName + '.css');
			// console.log(viewPath)
		} else {
			viewPath = path.join(config.viewPath, ViewName + '.html');
			// console.log(viewPath);
		}
		fs.readFile(viewPath, function(err, data) {
			if (err) {
				return res.end('404 not found');
			}
			// 通过underScore的template渲染data，会返回一个函数
			var compile = underscore.template(data.toString());
			var htmlStr = compile(dataobj);
			// 将渲染好的html字符串,返回给前端展示1
			res.end(htmlStr);
		});
	}
}
